<template>
  <div id="messTopSearch">
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="formInline.createTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="发布类型" prop="sourceType">
          <el-select
            v-model="formInline.sourceType"
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in publishTypeList"
              :key="item.dictLabel"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="触达渠道" prop="touchType">
          <el-select v-model="formInline.touchType" clearable>
            <el-option
              v-for="item in touchChannelList"
              :key="item.dictLabel"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 标题部分 -->
        <el-form-item label="查询:" prop="searchContent">
          <el-input
            clearable
            v-model="formInline.searchContent"
            placeholder="标题.通知ID查询"
            size="small"
            class="numberInput"
          ></el-input>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
export default {
  name: 'MessTopSearch',
  msg: '角色管理-顶部搜索',
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          sourceType: '', // 发布类型,默认全部
          touchType: '', // 触达渠道,默认全部
          searchContent: '', // 查询
          createTime: [],
          startTime: '', // 开始时间
          endTime: '' // 结束时间
        }
      }
    }
  },
  data() {
    return {
      // 发布类型列表
      publishTypeList: [{ dictLabel: '全部', dictValue: '' }],
      // 触达渠道列表
      touchChannelList: [{ dictLabel: '全部', dictValue: '' }],
      rules: {
        sourceType: {},
        touchType: {},
        searchContent: {},
        createTime: { type: 'array' }
      }
    }
  },
  watch: {
    'formInline.createTime': {
      handler(newVal, oldVal) {
        if (newVal && newVal.length > 1) {
          this.formInline.startTime = newVal[0]
          this.formInline.endTime = newVal[1]
        } else {
          this.formInline.startTime = ''
          this.formInline.endTime = ''
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getStatusList()
  },
  methods: {
    /* 搜索 */
    toSearch(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('toSearch')
        } else {
          return false
        }
      })
    },
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
    },
    async getStatusList() {
      // 触达渠道
      let touch = await sysNormalDisable('activity_notice_type', 'touchType')
      this.touchChannelList = this.touchChannelList.concat(touch)
      // 发布类型
      let Publish = await sysNormalDisable('notice_publish_type', 'pubType')
      this.publishTypeList = this.publishTypeList.concat(Publish)
    }
  }
}
</script>

<style lang="less">
#messTopSearch {
  .search-box {
    padding-left: 30px !important;
  }
  .activeType {
    width: 90px;
  }

  .touchChannel {
    width: 180px;
  }

  .title {
    width: 300px;
  }

  .s-btn .el-button {
    padding: 5px 18px !important;
  }
}
</style>
